#函数的嵌套
# def func(name):
#     # name="alex"
#     def inner():
#         print(name)
#         return 'luffy'
#     return inner
#
# v1 = func('武沛齐')()
# print(v1)
#
# v2 = func('alex')()
# print(v2)

# def func(name):
#     def inner():
#         print(name)
#         return 'luffy'
#     return inner
#
# v1 = func('武沛齐')
# v2 = func('alex')
# v1()
# v2()

# def func(name=None):
#     if not name:
#         name= '武沛齐'
#     def inner():
#         print(name)
#         return 'root'
#     return inner
#
# v1 = func()()
# v2 = func('alex')()
# print(v1,v2)
#
# #武沛齐
# #alex
# #root root

#函数闭包的使用
#封装数据防止污染全局
# def func(age):
#     name = "武沛齐"
#     def f1():
#         print(name, age)
#     def f2():
#         print(name, age)
#     def f3():
#         print(name, age)
#
#     f1()
#     f2()
#     f3()
#
# func(123)


# #封装到一个函数中，使用的时候再去闭包中去取
# def task(src):
#     def inner():
#         print(src)
#     return inner
#
# task('hello')()

#闭包结合更复杂的使用
# def task(arg):
#     def inner():
#         print(arg)
#
#     return inner
#
#
# inner_func_list = []
# for val in [11, 22, 33]:
#     inner_func_list.append(task(val))
#
# inner_func_list[0]()  # 11
# inner_func_list[1]()  # 22
# inner_func_list[2]()  # 33

#装饰器
# 现在给你一个函数，在不修改函数源码的前提下，实现在函数执行前和执行后分别输入 “before” 和 “after”。
# def func():
#     print("我是func函数")
#     value = (11, 22, 33, 44)
#     return value
# result = func()
# print(result)


# def func():
#     print("before")
#     print("我是func函数")
#     value = (11, 22, 33, 44)
#     print(value)
#     print("after")
#     return value
#
# result = func()

#更高级 一点的写法
# def func():
#     print("我是func函数")
#     value = (11, 22, 33, 44)
#     return value
#
#
# def outer(origin):
#     def inner():
#         print('inner')
#         origin()
#         print("after")
#     return inner
#
# result = outer(func)()
# print()

# def func():
#     print("我是func函数")
#     value = (11, 22, 33, 44)
#     return value
# def outer(origin):
#     def inner():
#         print('inner')
#         res = origin()
#         print("after")
#         return res  #保持和原函数一样的效果
#     return inner
#
# result = outer(func)()


# def outer(origin):
#     def inner():
#         print('before')
#         res = origin()
#         print("after")
#         return res
#     return inner
#
# @outer
# def func():
#     print("我是func函数")
#     value = (11, 22, 33, 44)
#     return value
#
# func()


# def outer(org):
#     def inner():
#         print("before")
#         res = org()
#         print("after")
#         return res
#     return inner
#
# @outer
# def func1():
#     print("我是func1函数")
#     value = (11, 22, 33, 44)
#     return value
#
# @outer
# def func2():
#     print("我是func2函数")
#     value = (11, 22, 33, 44)
#     return value
#
# @outer
# def func3():
#     print("我是func3函数")
#     value = (11, 22, 33, 44)
#     return value
#
# func1()
# func2()
# func3()
